﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Recrystallization
{
    public class clsDeployment
    {
        private double ro, roTMP, averageDELTA_ro;
        public double critical_sigma;
        private double sigma, sigmaTMP, averageDELTA_sigma;

        const double A = 86710969050178.5, B = 9.41268203527779;
        private Random rand;

        public clsDeployment()
        {
            ro = 1; 
            sigma = 1.9 * 2.57 * 8 * Math.Sqrt(ro);
            rand = new Random();
        }

        public clsDeployment(int N, int M)
        {
            ro = 1;
            sigma = 1.9 * 2.57 * 8 * Math.Sqrt(ro);
            critical_sigma = 8 * Math.Pow(10, 7) / N * M;
            //critical_sigma = 4.21584014232342 * (double)Math.Pow(10, 12) / (double)N * (double)M;
            rand = new Random();
        }

        public void resetVariable()
        {
            ro = 1;
            sigma = 1.9 * 2.57 * 8 * Math.Sqrt(ro);
            roTMP = 0;
            averageDELTA_ro = 1;
        }

        private void calculateRo(double time)
        {
            roTMP = A / B + (1 - A / B) * Math.Exp(-B * time);
            sigmaTMP = 1.9 * 2.57 * 8 * Math.Sqrt(roTMP);
        }

        public void calculateAverageDELTAro(int N, int M, double time)
        {
            calculateRo(time);
            //double DELTA_ro = (roTMP - ro);
            double DELTA_sigma = sigmaTMP - sigma;
            double area = N * M;
            //averageDELTA_ro = DELTA_ro / area;
            averageDELTA_sigma = DELTA_sigma / area;
            ro = roTMP;
            sigma = sigmaTMP;
        }

        public void calculateCRITICALro(int N, int M)
        {
            double area = N * M;
            critical_sigma = 8 * Math.Pow(10, 7) / area;
        }

        public double getDeploymentForGrain(bool onBorder)
        {
            if (onBorder)
            {
                return (1.0 - 0.1 * (double)rand.Next(0, 3)) * averageDELTA_sigma;
            }
            else return (1.0 - 0.1 * (double)rand.Next(7, 10)) * averageDELTA_sigma;
        }
    }
}
